﻿using AutoMapper;
using Sheep_Breeding.Domain;
using Sheep_Breeding.ErrorCode;
using Sheep_Breeding.Infeartructure;
using Sheep_Breeding.Read.Api.Dtos;
using Sheep_Breeding.Read.Api.OperationServices.IServices;

namespace Sheep_Breeding.Read.Api.OperationServices.Services
{
    /// <summary>
    /// 羊只档案服务实现
    /// </summary>
    public class SheepFilesServices : ISheepFilesServices
    {
        private readonly IBaseRepository<SheepFiles> sheepFilesRepository;
        private readonly ILogger<SheepFilesServices> logger;
        private readonly IMapper mapper;

        public SheepFilesServices(IBaseRepository<SheepFiles> sheepFilesRepository, ILogger<SheepFilesServices> logger, IMapper mapper)
        {
            this.sheepFilesRepository = sheepFilesRepository;
            this.logger = logger;
            this.mapper = mapper;
        }

        public async Task<ApiResult<ApiPaging<List<SheepFiles>>>> GetList(SheepFilesQueryDto queryDto)
        {
            try
            {
                logger.LogInformation("开始获取羊只档案列表");
                var res = new ApiResult<ApiPaging<List<SheepFiles>>>();
                var query = sheepFilesRepository.Getlist().Where(x => !x.IsDeleted);

                // 动态条件筛选
                if (!string.IsNullOrEmpty(queryDto.EarNumber))
                    query = query.Where(x => x.EarNumber.Contains(queryDto.EarNumber));
                if (!string.IsNullOrEmpty(queryDto.ELEarNumber))
                    query = query.Where(x => x.ELEarNumber.Contains(queryDto.ELEarNumber));
                if (!string.IsNullOrEmpty(queryDto.Variety))
                    query = query.Where(x => x.Variety.Contains(queryDto.Variety));
                if (!string.IsNullOrEmpty(queryDto.Sex))
                    query = query.Where(x => x.Sex == queryDto.Sex);
                if (!string.IsNullOrEmpty(queryDto.GrowthStage))
                    query = query.Where(x => x.GrowthStage == queryDto.GrowthStage);
                if (queryDto.BuildingId > 0)
                    query = query.Where(x => x.BuildingId == queryDto.BuildingId);
                if (queryDto.Field > 0)
                    query = query.Where(x => x.Field == queryDto.Field);
                if (!string.IsNullOrEmpty(queryDto.SheepType))
                    query = query.Where(x => x.SheepType == queryDto.SheepType);
                if (!string.IsNullOrEmpty(queryDto.ReproductiveStatus))
                    query = query.Where(x => x.ReproductiveStatus == queryDto.ReproductiveStatus);
                if (!string.IsNullOrEmpty(queryDto.ProductionGrade))
                    query = query.Where(x => x.ProductionGrade == queryDto.ProductionGrade);
                if (!string.IsNullOrEmpty(queryDto.SalesLevel))
                    query = query.Where(x => x.SalesLevel == queryDto.SalesLevel);
                if (!string.IsNullOrEmpty(queryDto.GeneticLevel))
                    query = query.Where(x => x.GeneticLevel == queryDto.GeneticLevel);
                if (!string.IsNullOrEmpty(queryDto.FatherEarNumber))
                    query = query.Where(x => x.FatherEarNumber == queryDto.FatherEarNumber);
                if (!string.IsNullOrEmpty(queryDto.MotherEarNumber))
                    query = query.Where(x => x.MotherEarNumber == queryDto.MotherEarNumber);
                if (queryDto.AgeByDay > 0)
                    query = query.Where(x => x.AgeByDay == queryDto.AgeByDay);
                if (queryDto.AgeByMonth > 0)
                    query = query.Where(x => x.AgeByMonth == queryDto.AgeByMonth);
                if (queryDto.PregnancyDays > 0)
                    query = query.Where(x => x.PregnancyDays == queryDto.PregnancyDays);

                // 总数
                var totalCount = query.Count();
                // 分页
                int pageIndex = queryDto.PageIndex > 0 ? queryDto.PageIndex : 1;
                int pageSize = queryDto.PageSize > 0 ? queryDto.PageSize : 10;
                var pageData = query.OrderByDescending(x => x.SheepFilesId)
                    .Skip((pageIndex - 1) * pageSize)
                    .Take(pageSize)
                    .ToList();
                // 封装分页结果
                var paging = new ApiPaging<List<SheepFiles>>
                {
                    TotalCount = totalCount,
                    TotalPage = (int)Math.Ceiling(totalCount * 1.0 / pageSize),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    PageData = pageData
                };
                res.code = ApiREunm.Ok;
                res.msg = "获取羊只档案列表成功";
                res.data = paging;
                return res;
            }
            catch (Exception ex)
            {
                logger.LogError("获取羊只档案列表异常：" + ex.Message);
                return new ApiResult<ApiPaging<List<SheepFiles>>>
                {
                    code = ApiREunm.Fail,
                    msg = "获取羊只档案列表失败",
                    data = null
                };
            }
        }
    }
}